package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.EquipmentConfig;
import com.ruoyi.baseData.domain.EquipmentLevel;
import com.ruoyi.baseData.domain.dto.EquipmentLevelDto;
import com.ruoyi.baseData.domain.vo.EquipmentLevelVo;
import com.ruoyi.baseData.mapper.EquipmentLevelMapper;
import com.ruoyi.baseData.service.EquipmentLevelService;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
*
*/
@Service
public class EquipmentLevelServiceImpl implements EquipmentLevelService {



    @Resource
    EquipmentLevelMapper equipmentLevelMapper;

    @Override
    public Page<EquipmentLevelVo> getEquipmentLevelByPage(EquipmentLevelDto pageEntity) {
        //todo:添加查询条件
        MPJLambdaWrapper<EquipmentLevel> wrapper = new MPJLambdaWrapper<EquipmentLevel>()
                .selectAll(EquipmentLevel.class)//查询user表全部字段
                //记的测试阶段
                .like(StringUtils.isNotBlank(pageEntity.getName()),EquipmentLevel::getName, pageEntity.getName())
                .like(StringUtils.isNotBlank(pageEntity.getCode()),EquipmentLevel::getCode, pageEntity.getCode())
                .eq(EquipmentLevel::getDelFlag, 0)
                .orderByDesc(EquipmentLevel::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, EquipmentLevel.class, pageEntity);
        Page<EquipmentLevelVo> userList = equipmentLevelMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   EquipmentLevelVo.class, wrapper);
        return userList;
    }

    @Override
    public void addEquipmentLevel(EquipmentLevelDto testDto) {

//        Long userid = SecurityUtils.getLoginUser().getSysUser().getDept();
        String userName = SecurityUtils.getUsername();
        String permission = SecurityContextHolder.getPermission();
        String token = SecurityUtils.getToken();
        LoginUser loginUser = new LoginUser();
        if (StringUtils.isNotEmpty(token)) {
            loginUser = AuthUtil.getLoginUser(token);
        }
        MPJLambdaWrapper<EquipmentLevel> wrapper = new MPJLambdaWrapper<EquipmentLevel>()
                .eq(EquipmentLevel::getCode, testDto.getCode())
                .ne(EquipmentLevel::getDelFlag, 1);

        EquipmentLevel material = equipmentLevelMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        EquipmentLevel test2 = new EquipmentLevel();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        //添加用户
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        equipmentLevelMapper.insert(test2);
    }

    @Override
    public void updateEquipmentLevel(EquipmentLevelDto testDto) {
        MPJLambdaWrapper<EquipmentLevel> wrapper = new MPJLambdaWrapper<EquipmentLevel>()
                .eq(EquipmentLevel::getCode, testDto.getCode());
        EquipmentLevel material = equipmentLevelMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        equipmentLevelMapper.updateById(material);
    }

    @Override
    public void delEquipmentLevel(EquipmentLevelDto testDto) {
        EquipmentLevel material = equipmentLevelMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        equipmentLevelMapper.updateById(material);
    }
}
